// pages/volunteer/mall.js
Page({
  data: {
    // 用户信息
    userInfo: {
      name: '晴天',
      volunteerId: '123444890',
      currentPoints: 185
    },
    
    // 商品列表
    products: [
      {
        id: 1,
        name: '马克杯',
        image: '/images/杯子.png',
        points: 50,
        canRedeem: true,
        description: '精美白色陶瓷马克杯，容量350ml',
        stock: 100,
        category: 'daily'
      },
      {
        id: 2,
        name: '马克杯',
        image: '/images/1.jpg',
        points: 60,
        canRedeem: true,
        description: '时尚青色马克杯，带拉花图案',
        stock: 80,
        category: 'daily'
      },
      {
        id: 3,
        name: '马克杯',
        image: '/images/杯子.png',
        points: 80,
        canRedeem: false,
        description: '金色手柄马克杯，高端品质',
        stock: 50,
        category: 'daily'
      },
      {
        id: 4,
        name: '马克杯',
        image: '/images/杯子.png',
        points: 70,
        canRedeem: true,
        description: '棕色陶瓷杯套装，含杯托',
        stock: 60,
        category: 'daily'
      },
      {
        id: 5,
        name: '故事集',
        image: '/images/杯子.png',
        points: 120,
        canRedeem: false,
        description: '遗体捐献者感人故事集',
        stock: 30,
        category: 'book'
      },
      {
        id: 6,
        name: '雨伞',
        image: '/images/杯子.png',
        points: 100,
        canRedeem: true,
        description: '定制logo雨伞，防水耐用',
        stock: 40,
        category: 'daily'
      }
    ],
    
    // 页面状态
    showPointsTip: false,
    hasMore: false,
    loading: false,
    currentPage: 1,
    pageSize: 10
  },

  onLoad(options) {
    this.loadUserData();
    this.checkRedeemStatus();
    this.checkShowPointsTip();
  },

  onShow() {
    this.refreshData();
  },

  // 加载用户数据
  loadUserData() {
    const userData = wx.getStorageSync('volunteerUserData');
    if (userData) {
      this.setData({
        userInfo: {
          ...this.data.userInfo,
          ...userData
        }
      });
    }
  },

  // 检查兑换状态
  checkRedeemStatus() {
    const products = this.data.products.map(product => {
      const canRedeem = this.data.userInfo.currentPoints >= product.points && product.stock > 0;
      return { ...product, canRedeem };
    });
    
    this.setData({ products });
  },

  // 检查是否显示积分提示
  checkShowPointsTip() {
    const hasInsufficientPoints = this.data.products.some(product => 
      this.data.userInfo.currentPoints < product.points
    );
    
    if (hasInsufficientPoints) {
      this.setData({ showPointsTip: true });
    }
  },

  // 刷新数据
  refreshData() {
    this.loadUserData();
    this.checkRedeemStatus();
  },

  // 查看商品详情
  viewProduct(e) {
    const productId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/volunteer/product-detail?id=${productId}`
    });
  },

  // 兑换商品
  redeemProduct(e) {
    const productId = e.currentTarget.dataset.id;
    const product = this.data.products.find(p => p.id === productId);
    
    if (!product) {
      wx.showToast({
        title: '商品不存在',
        icon: 'none'
      });
      return;
    }
    
    if (!product.canRedeem) {
      if (this.data.userInfo.currentPoints < product.points) {
        wx.showToast({
          title: `积分不足，需要${product.points}积分，当前${this.data.userInfo.currentPoints}积分`,
          icon: 'none',
          duration: 3000
        });
      } else if (product.stock <= 0) {
        wx.showToast({
          title: '商品库存不足',
          icon: 'none'
        });
      }
      return;
    }
    
    // 检查用户是否已登录
    const userData = wx.getStorageSync('volunteerUserData') || {};
    if (!userData.name || !userData.volunteerId) {
      wx.showModal({
        title: '提示',
        content: '请先完善个人信息',
        confirmText: '去完善',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/volunteer/profile'
            });
          }
        }
      });
      return;
    }
    
    // 检查是否有收货地址
    const addressList = wx.getStorageSync('volunteerAddresses') || [];
    if (addressList.length === 0) {
      wx.showModal({
        title: '提示',
        content: '请先添加收货地址',
        confirmText: '去添加',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/volunteer/address'
            });
          }
        }
      });
      return;
    }
    
    // 跳转到兑换流程页面
    wx.navigateTo({
      url: `/pages/volunteer/redeem-flow?productId=${productId}`
    });
  },

  // 显示可兑换商品
  showRedeemableProducts() {
    const redeemableProducts = this.data.products.filter(product => product.canRedeem);
    
    if (redeemableProducts.length === 0) {
      wx.showToast({
        title: '暂无可兑换商品',
        icon: 'none'
      });
      return;
    }
    
    // 这里可以跳转到一个只显示可兑换商品的页面
    wx.showToast({
      title: `找到${redeemableProducts.length}个可兑换商品`,
      icon: 'none'
    });
  },

  // 关闭积分提示
  closePointsTip() {
    this.setData({ showPointsTip: false });
  },

  // 跳转到积分明细
  goToPointsDetail() {
    wx.navigateTo({
      url: '/pages/volunteer/points'
    });
  },

  // 跳转到地址管理
  goToAddress() {
    wx.navigateTo({
      url: '/pages/volunteer/address'
    });
  },

  // 跳转到订单管理
  goToOrders() {
    wx.navigateTo({
      url: '/pages/volunteer/orders'
    });
  },

  // 加载更多商品
  loadMore() {
    if (this.data.loading || !this.data.hasMore) {
      return;
    }
    
    this.setData({ loading: true });
    
    // 模拟加载更多数据
    setTimeout(() => {
      // 这里应该调用实际的API
      this.setData({
        loading: false,
        hasMore: false // 暂时设置为false，实际应该根据API返回结果判断
      });
    }, 1000);
  },

  // 防止点击事件冒泡
  preventTap() {
    // 空函数，用于阻止事件冒泡
  }
});